package com.hua.kafakTest.controller;


import com.alibaba.fastjson.JSONObject;
import com.hua.kafakTest.entity.Device;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping(value = "/kafka/testSend")
    public void sendMessage(String topic,int num) {
        for (int i =1;i<=num;i++) {
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            Device device = new Device(i,uuid,false);
            String s = JSONObject.toJSONString(device);
            kafkaTemplate.send(topic,s).addCallback(success ->{
                System.out.println("发送成功");
            },fail->{
                System.out.println("发送失败"+s);
            });
        }

    }

}
